package org.ponly.cta4j.drawing;

import java.awt.Font;
import java.awt.image.BufferedImage;
import java.text.AttributedString;
import org.ponly.cta4j.drawing.background.BackgroundBrush;
import org.ponly.cta4j.drawing.background.ColorBackgroundBrush;
import org.ponly.cta4j.drawing.font.FontFamily;
import org.ponly.cta4j.drawing.font.TransformRandomFontFamily;
import org.ponly.cta4j.drawing.text.GlyphsBrush;
import org.ponly.cta4j.drawing.text.TextBrush;

/**
 * 通用 Painter.<br/>
 * <dl>
 * <dt>GeneralPainter 主要有三部分组成: </dt>
 * <dd>1. 背景笔刷 ({@link BackgroundBrush}): 负责背景的绘制.</dd>
 * <dd>2. 文本笔刷 ({@link TextBrush}): 负责文本的绘制.</dd>
 * <dd>3. 字体库 ({@link FontFamily}): 负责提供文本绘制的字体.</dd>
 * </dl>
 *
 * @author vacoor
 * @since 1.0
 */
public class GeneralPainter extends AbstractPainter {
    /**
     * 背景笔刷.
     */
    private BackgroundBrush backgroundBrush;

    /**
     * 文本笔刷.
     */
    private TextBrush textBrush;

    /**
     * 字体库.
     */
    private FontFamily fontFamily;

    /**
     * 构建一个默认 Painter.
     */
    public GeneralPainter() {
        this.backgroundBrush = new ColorBackgroundBrush();
        this.textBrush = new GlyphsBrush();
        this.fontFamily = new TransformRandomFontFamily(true, true);
    }

    /**
     * 使用给定的配置构建一个 Painter.
     *
     * @param backgroundBrush 背景笔刷
     * @param textBrush       文本笔刷
     * @param fontFamily      字体库
     */
    public GeneralPainter(final BackgroundBrush backgroundBrush, final TextBrush textBrush, final FontFamily fontFamily) {
        this.backgroundBrush = backgroundBrush;
        this.textBrush = textBrush;
        this.fontFamily = fontFamily;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected BufferedImage doPaintBackground(final BufferedImage canvas) {
        return backgroundBrush.draw(canvas);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected BufferedImage doPaintText(final BufferedImage canvas, final AttributedString attributedString) {
        return textBrush.draw(canvas, attributedString);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Font getFont() {
        return fontFamily.getFont();
    }

    @Override
    public int getMaxTextLength() {
        return getTextBrush().getMaxTextLength();
    }

    @Override
    public int getMinTextLength() {
        return getTextBrush().getMinTextLength();
    }

    /* ****************************
     *      GETTER / SETTER
     * ****************************/

    /**
     * @return 获取当前使用的字体库
     */
    public FontFamily getFontFamily() {
        return fontFamily;
    }

    /**
     * 设置字体库.
     *
     * @param fontFamily 新的字体库
     */
    public void setFontFamily(final FontFamily fontFamily) {
        this.fontFamily = fontFamily;
    }

    /**
     * @return 获取当前使用的背景笔刷
     */
    public BackgroundBrush getBackgroundBrush() {
        return backgroundBrush;
    }

    /**
     * 设置背景笔刷.
     *
     * @param backgroundBrush 新的背景笔刷
     */
    public void setBackgroundBrush(final BackgroundBrush backgroundBrush) {
        this.backgroundBrush = backgroundBrush;
    }

    /**
     * @return 获取当前使用的文本笔刷
     */
    public TextBrush getTextBrush() {
        return textBrush;
    }

    /**
     * 设置文本笔刷.
     *
     * @param textBrush 新的文本笔刷
     */
    public void setTextBrush(final TextBrush textBrush) {
        this.textBrush = textBrush;
    }
}
